package com.noble.meether.ui.invite.exchange;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.noble.meether.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.log.PToast;

import org.json.JSONObject;

/**
 * 男性：邀请奖励兑换钻石
 */
public class InviteExchangeAct extends BaseAppCompatActivity implements RequestComplete, View.OnClickListener {
    private TextView tv_money, tv_num;
    private EditText et_num;

    private boolean isExchangeSuc = false;   // 是否成功进行过兑换
    private double amount;  // 用户邀请金额
    private int doExchange; // 可兑换钻石数

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_invite_exchange);
        initView();
    }

    private void initView() {
        setBackView("兑换");
        tv_money = findViewById(R.id.tv_money);
        tv_num = findViewById(R.id.tv_num);
        et_num = findViewById(R.id.et_num);
        findViewById(R.id.btn_exchange).setOnClickListener(this);
        reqData();
    }

    private void reqData() {
        ModuleMgr.getCommonMgr().reqExchangeRule(this);
    }

    private void refreshView() {
        tv_money.setText(String.valueOf(amount));
        tv_num.setText(String.valueOf(doExchange));
    }

    private void submit() {
        String num = et_num.getText().toString();
        if (TextUtils.isEmpty(num)) {
            PToast.showShort("请输入兑换金额");
            return;
        }

        int number = Integer.valueOf(num);
        if (number > amount) {
            PToast.showShort("兑换金额超过最大值");
            return;
        }
        ModuleMgr.getCommonMgr().reqInviteExchange(number, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqExchangeRule) {
            if (!response.isOk()) return;
            JSONObject jsonObject = response.getResponseObject();
            amount = jsonObject.optDouble("Amount");
            doExchange = jsonObject.optInt("DoExchange");
            refreshView();
            return;
        }

        if (response.getUrlParam() == UrlParam.reqInviteExchange) {
            if (!response.isOk()) {
                PToast.showShort("兑换失败");
                return;
            }
            reqData();
            et_num.setText("");
            isExchangeSuc = true;
            PToast.showShort("兑换成功");
        }
    }

    @Override
    public void back() {
        super.back();
        if (isExchangeSuc)
            ModuleMgr.getCenterMgr().reqMyInfo("InviteExchange");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_exchange:
                submit();
                break;
        }
    }
}